package org.wk.user.service;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.wk.account.service.SubAccountZuanService;
import org.wk.core.common.view.ResponseVo;
import org.wk.core.db.tool.WebUtils;
import org.wk.user.dao.AttendancesDao;
import org.wk.user.domain.Attendances;
import org.wk.user.enums.AttendancesStatus;
import org.wk.user.enums.ResponseCode;

/**
 * @ClassName AttendancesServiceImpl
 * @Description 签到
 * @Author ZhaoPing'an
 * @Date 2018/5/21 15:55
 */
@Service
public class AttendancesServiceImpl implements AttendancesService{

    @Autowired
    private AttendancesDao attendancesdao;

    @Autowired
    private SubAccountZuanService zuanService;

    /**
     * @Description 获取用户签到状态
     * @Param
     * @author ZhaoPing'an
     * @Date 2018/5/21 15:16
     */
    @Override
    public ResponseVo getSignStatus() {
        String userId = WebUtils.getUser().getId();
        JSONObject json = new JSONObject();
        Attendances attendances = attendancesdao.findBySignDateAndUserId(new DateTime().toLocalDate().toDate(),userId);
        if (StringUtils.isEmpty(attendances)){
            json.put("status_code",AttendancesStatus.NOT_SIGN.getCode());
            json.put("status_name",AttendancesStatus.NOT_SIGN.getName());
        } else {
            json.put("status_code",AttendancesStatus.HAS_SIGN.getCode());
            json.put("status_name",AttendancesStatus.HAS_SIGN.getName());
        }
        return ResponseVo.success().setData(json);
    }

    /**
     * @Description 签到
     * @Param []
     * @author ZhaoPing'an
     * @Date 2018/5/21 16:34
     */
    @Override
    public ResponseVo sign() {
        String userId = WebUtils.getUser().getId();
        Attendances sign = attendancesdao.findBySignDateAndUserId(new DateTime().toLocalDate().toDate(),userId);
        if (!StringUtils.isEmpty(sign)){
            return new ResponseVo(ResponseCode.TODAY_HAS_SIGN);
        }
        Attendances attendances = new Attendances();
        attendances.setUserId(userId);
        attendances.setSignDate(new DateTime().toLocalDate().toDate());
        attendancesdao.save(attendances);
        // 签到成功赠送鲸钻
        zuanService.attendancesAward(userId);
        return ResponseVo.success();
    }
}
